package com.zxl.common.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zxl.common.exception.BusinessException;
import com.zxl.common.mapper.SysShoppingCartMapper;
import com.zxl.common.model.bo.shopping.ShoppingDelete;
import com.zxl.common.model.bo.shopping.ShoppingEdit;
import com.zxl.common.model.pojo.SysGoods;
import com.zxl.common.model.pojo.SysShoppingCart;
import com.zxl.common.model.vo.goods.GoodsCartVo;
import com.zxl.common.service.SysGoodsService;
import com.zxl.common.service.SysShoppingCartService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;

/**
 * <p>
 * 购物车 服务实现类
 * </p>
 *
 * @author zxw
 * @since 2023-03-30
 */
@Service
public class SysShoppingCartServiceImpl extends ServiceImpl<SysShoppingCartMapper, SysShoppingCart> implements SysShoppingCartService {

    @Resource
    private SysGoodsService sysGoodsService;

    @Resource
    private SysShoppingCartService shoppingCartService;

    @Resource
    private SysShoppingCartMapper sysShoppingCartMapper;



    @Override
    public void add(ShoppingEdit shoppingEdit) {
        Integer memberId = shoppingEdit.getMemberId();
        String goodCode = shoppingEdit.getGoodCode();
        SysGoods sysGoods = sysGoodsService.getOne(new QueryWrapper<SysGoods>()
                .eq("goods_code", goodCode));
        if (sysGoods.getGoodsStock() <=0){
            throw new BusinessException("商品库存不足");
        }
        SysShoppingCart sysShoppingCart = sysShoppingCartMapper.selectOne(new QueryWrapper<SysShoppingCart>()
                .eq("member_id", memberId)
                .eq("goods_code", goodCode));
         /**  如果当前购物车没有此项目, 创建新条目  */
        if (sysShoppingCart == null){
            createShopping(sysGoods, shoppingEdit.getMemberId());
        }else {
            if (sysGoods.getGoodsStock() != null && sysGoods.getGoodsStock() <= sysShoppingCart.getGoodsNum()){
                throw new BusinessException(sysGoods.getGoodsName() + "商品库存不足");
            }
            /**  如果购物车已有此项目, 数量+1  **/
            sysShoppingCart.setGoodsNum(sysShoppingCart.getGoodsNum() + 1);
            sysShoppingCart.setTotalPrice(sysShoppingCart.getGoodsPrice()
                    .multiply(new BigDecimal(sysShoppingCart.getGoodsNum())));
            sysShoppingCartMapper.update(null, new UpdateWrapper<SysShoppingCart>()
                    .eq("member_id",memberId)
                    .eq("goods_code",sysShoppingCart.getGoodsCode())
                    .set("goods_num",sysShoppingCart.getGoodsNum())
                    .set("total_price",sysShoppingCart.getTotalPrice()));
        }

    }

    @Override
    public void lessen(ShoppingEdit shoppingEdit) {
        Integer memberId = shoppingEdit.getMemberId();
        String goodCode = shoppingEdit.getGoodCode();
        SysGoods sysGoods = sysGoodsService.getOne(new QueryWrapper<SysGoods>()
                .eq("goods_code", shoppingEdit.getGoodCode()));
        SysShoppingCart sysShoppingCart = sysShoppingCartMapper.selectOne(new QueryWrapper<SysShoppingCart>()
                .eq("member_id", memberId)
                .eq("goods_code", goodCode));
        /**  减少到0后删除  **/
        if (sysShoppingCart.getGoodsNum() -1 <= 0){
            deleteIndentItem(goodCode, shoppingEdit.getMemberId());
        }
        if (sysGoods.getGoodsStock() <= shoppingEdit.getGoodsNum()){
            BigDecimal totalPrice = sysGoods.getGoodsPrice().multiply(new BigDecimal(sysGoods.getGoodsStock()));
            sysShoppingCartMapper.updateCartGoodsNum(memberId,goodCode,sysGoods.getGoodsStock(),totalPrice);
        }else {
            sysShoppingCart.setGoodsNum(sysShoppingCart.getGoodsNum() - 1);
            sysShoppingCart.setTotalPrice(sysShoppingCart.getGoodsPrice()
                    .multiply(new BigDecimal(sysShoppingCart.getGoodsNum())));
            sysShoppingCartMapper.updateCartGoodsNum(memberId,goodCode,sysShoppingCart.getGoodsNum(),sysShoppingCart.getTotalPrice());
        }

    }

    @Override
    public List<GoodsCartVo> delete(ShoppingDelete shoppingDelete) {
        Integer memberId = shoppingDelete.getMemberId();
        List<String> goodsCodes = shoppingDelete.getGoodsCode();
        sysShoppingCartMapper.deleteByGoodsCode(memberId,goodsCodes);
        List<GoodsCartVo> memberGoodsCartList = sysShoppingCartMapper.getMemberGoodsCart(memberId);
        return memberGoodsCartList;
    }

    @Override
    public void updateNum(Integer memberId,String goodsCode, Integer goodsNum) {
        SysGoods sysGoods = sysGoodsService.getOne(new QueryWrapper<SysGoods>()
                .eq("goods_code", goodsCode));
        if (sysGoods.getGoodsStock() != null && sysGoods.getGoodsStock() <= goodsNum){
            throw new BusinessException("商品最多只能购买：" + sysGoods.getGoodsStock() + "个");
        }
        BigDecimal totalPrice = sysGoods.getGoodsPrice()
                .multiply(new BigDecimal(goodsNum));
        sysShoppingCartMapper.update(null,new UpdateWrapper<SysShoppingCart>()
                .eq("member_id", memberId)
                .eq("goods_code", goodsCode)
                .set("goods_num", goodsNum)
                .set("total_price", totalPrice));
    }

    @Override
    public List<GoodsCartVo> getMemberGoodsCart(Integer memberId) {
        List<GoodsCartVo> memberGoodsCartList = sysShoppingCartMapper.getMemberGoodsCart(memberId);
        return memberGoodsCartList;
    }


    public void createShopping(SysGoods sysGoods, Integer memberId){
        SysShoppingCart sysShoppingCart = new SysShoppingCart();
        sysShoppingCart.setMemberId(memberId);
        sysShoppingCart.setGoodsCode(sysGoods.getGoodsCode());
        sysShoppingCart.setGoodsName(sysGoods.getGoodsName());
        sysShoppingCart.setGoodsPrice(sysGoods.getGoodsPrice());
        sysShoppingCart.setTotalPrice(sysGoods.getGoodsPrice());
        sysShoppingCart.setGoodsNum(1);
        sysShoppingCartMapper.insert(sysShoppingCart);
    }

    public void deleteIndentItem(String goodsCode, Integer memberId){
        SysShoppingCart shoppingCart = shoppingCartService.getOne(new QueryWrapper<SysShoppingCart>()
                .eq("member_id", memberId)
                .eq("goods_code", goodsCode));
        shoppingCartService.removeById(shoppingCart.getId());
    }
}
